1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import java.applet.Applet;
34 import java.awt.Color;
35 import java.awt.Font;
36 import java.awt.Graphics;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 import java.util.Locale;
40
41
42
43
44
45
46
47
48 @SuppressWarnings("serial")
49 public class Clock extends Applet implements Runnable {
50
51 private volatile Thread timer;
52 private int lastxs, lastys, lastxm,
53 lastym, lastxh, lastyh;
54 private SimpleDateFormat formatter;
55 private String lastdate;
56 private Font clockFaceFont;
57 private Date currentDate;
58 private Color handColor;
59 private Color numberColor;
60 private int xcenter = 80, ycenter = 55;
61
62 @Override
63 public void init() {
64 lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
65 formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy",
66 Locale.getDefault());
67 currentDate = new Date();
68 lastdate = formatter.format(currentDate);
69 clockFaceFont = new Font("Serif", Font.PLAIN, 14);
70 handColor = Color.blue;
71 numberColor = Color.darkGray;
72
73 try {
74 setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
75 16)));
76 } catch (NullPointerException e) {
77 } catch (NumberFormatException e) {
78 }
79 try {
80 handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
81 16));
82 } catch (NullPointerException e) {
83 } catch (NumberFormatException e) {
84 }
85 try {
86 numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
87 16));
88 } catch (NullPointerException e) {
89 } catch (NumberFormatException e) {
90 }
91 resize(300, 300);
92 }
93
94
95
96
97 @Override
98 public void update(Graphics g) {
99 int xh, yh, xm, ym, xs, ys;
100 int s = 0, m = 10, h = 10;
101 String today;
102
103 currentDate = new Date();
104
105 formatter.applyPattern("s");
106 try {
107 s = Integer.parseInt(formatter.format(currentDate));
108 } catch (NumberFormatException n) {
109 s = 0;
110 }
111 formatter.applyPattern("m");
112 try {
113 m = Integer.parseInt(formatter.format(currentDate));
114 } catch (NumberFormatException n) {
115 m = 10;
116 }
117 formatter.applyPattern("h");
118 try {
119 h = Integer.parseInt(formatter.format(currentDate));
120 } catch (NumberFormatException n) {
121 h = 10;
122 }
123
124
125 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
126 ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
127 xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
128 ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
129 xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2)
130 * 30
131 + xcenter);
132 yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2)
133 * 30
134 + ycenter);
135
136
137 formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
138 today = formatter.format(currentDate);
139
140 g.setFont(clockFaceFont);
141
142 g.setColor(getBackground());
143 if (xs != lastxs || ys != lastys) {
144 g.drawLine(xcenter, ycenter, lastxs, lastys);
145 g.drawString(lastdate, 5, 125);
146 }
147 if (xm != lastxm || ym != lastym) {
148 g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
149 g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
150 }
151 if (xh != lastxh || yh != lastyh) {
152 g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
153 g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
154 }
155
156
157 g.setColor(numberColor);
158 g.drawString(today, 5, 125);
159 g.drawLine(xcenter, ycenter, xs, ys);
160 g.setColor(handColor);
161 g.drawLine(xcenter, ycenter - 1, xm, ym);
162 g.drawLine(xcenter - 1, ycenter, xm, ym);
163 g.drawLine(xcenter, ycenter - 1, xh, yh);
164 g.drawLine(xcenter - 1, ycenter, xh, yh);
165 lastxs = xs;
166 lastys = ys;
167 lastxm = xm;
168 lastym = ym;
169 lastxh = xh;
170 lastyh = yh;
171 lastdate = today;
172 currentDate = null;
173 }
174
175 @Override
176 public void paint(Graphics g) {
177 g.setFont(clockFaceFont);
178
179 g.setColor(handColor);
180 g.drawArc(xcenter - 50, ycenter - 50, 100, 100, 0, 360);
181 g.setColor(numberColor);
182 g.drawString("9", xcenter - 45, ycenter + 3);
183 g.drawString("3", xcenter + 40, ycenter + 3);
184 g.drawString("12", xcenter - 5, ycenter - 37);
185 g.drawString("6", xcenter - 3, ycenter + 45);
186
187
188 g.setColor(numberColor);
189 g.drawString(lastdate, 5, 125);
190 g.drawLine(xcenter, ycenter, lastxs, lastys);
191 g.setColor(handColor);
192 g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
193 g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
194 g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
195 g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
196 }
197
198 @Override
199 public void start() {
200 timer = new Thread(this);
201 timer.start();
202 }
203
204 @Override
205 public void stop() {
206 timer = null;
207 }
208
209 @Override
210 @SuppressWarnings("SleepWhileHoldingLock")
211 public void run() {
212 Thread me = Thread.currentThread();
213 while (timer == me) {
214 try {
215 Thread.sleep(100);
216 } catch (InterruptedException e) {
217 }
218 repaint();
219 }
220 }
221
222 @Override
223 public String getAppletInfo() {
224 return "Title: A Clock \n"
225 + "Author: Rachel Gollub, 1995 \n"
226 + "An analog clock.";
227 }
228
229 @Override
230 public String[][] getParameterInfo() {
231 String[][] info = {
232 { "bgcolor", "hexadecimal RGB number",
233 "The background color. Default is the color of your browser." },
234 { "fgcolor1", "hexadecimal RGB number",
235 "The color of the hands and dial. Default is blue." },
236 { "fgcolor2", "hexadecimal RGB number",
237 "The color of the second hand and numbers. Default is dark gray." }
238 };
239 return info;
240 }
241 }